const {
  getBanner,
  homeJGG,
  xjh_YHZU,
  xjh_XSMS,
  categories,
  pintuan,
  fzf_baopin,
  fzf_kanjia,
  fzf_categories,
} = require('../../http/api');
// index.js
// 获取应用实例
const app = getApp()
Page({
  data: {
    fzf_iSshow: false,
    fzf_show: false,
    fzf_listen: [],
    fzf_pt: [],
    motto: 'Hello World',
    userInfo: {},
    fzf_baopin: [],
    fzf_kanjia: [],
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    canIUseGetUserProfile: false,
    canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName'), // 如需尝试获取用户信息可改为false
    xjh_list: [],
    numm: 9,
    itemID: [],
    xjh_GGXQ:[],
    xjh_list1: {
      "data": [{
          "nick": "༺梧桐树༻",
          "uid": 1942058,
          "number": 2,
          "orderId": 859441,
          "avatarUrl": "https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTL0Uvr7ibj2cX7CdGquaEl1oEXz8mhgQx1CEibha139ic8ApKzBzmXYzk7pIib0H9QfHQiaGLglxFJcqWQ/132",
          "dateAdd": "2021-04-09 14:57:31",
          "goodsId": 805727,
          "goodsName": "兔毛马甲"
        },
        {
          "uid": 1945291,
          "number": 1,
          "orderId": 859429,
          "dateAdd": "2021-04-09 14:46:49",
          "goodsId": 805726,
          "goodsName": "风衣"
        },
        {
          "uid": 1945249,
          "number": 1,
          "orderId": 859423,
          "dateAdd": "2021-04-09 14:35:59",
          "goodsId": 805725,
          "goodsName": "貂绒毛衣"
        },
        {
          "uid": 1945255,
          "number": 1,
          "orderId": 859403,
          "dateAdd": "2021-04-09 14:19:55",
          "goodsId": 805724,
          "goodsName": "高品质的夹克外套"
        },
        {
          "uid": 1945255,
          "number": 2,
          "orderId": 859403,
          "dateAdd": "2021-04-09 14:19:55",
          "goodsId": 805723,
          "goodsName": "加绒毛衣，不久的北方就要穿到了"
        },
        {
          "nick": "gooking（api工厂创始人）",
          "uid": 1351478,
          "number": 1,
          "orderId": 805722,
          "avatarUrl": "https://cdn.it120.cc/apifactory/2019/06/26/9ea289ce-9ce7-4a2a-9342-25ae4abfd3f3.jpg",
          "dateAdd": "2021-04-09 13:27:50",
          "goodsId": 807491,
          "goodsName": "加绒毛衣，纯棉加绒款"
        },
        {
          "nick": "加绒毛衣，中大童",
          "uid": 1351478,
          "number": 1,
          "orderId": 859362,
          "avatarUrl": "https://cdn.it120.cc/apifactory/2019/06/26/5d19bb9b-4727-4fd3-9ab8-170ffde12bdc.jpg",
          "dateAdd": "2021-04-09 13:27:35",
          "goodsId": 805721,
          "goodsName": "云南大理原产沃柑 自家种植 现摘现发"
        },
        {
          "nick": "加绒毛衣，性价比高",
          "uid": 1942058,
          "number": 1,
          "orderId": 859351,
          "avatarUrl": "https://cdn.it120.cc/apifactory/2019/06/26/338e1ef3-c455-4873-ae9d-66ef22536251.jpg",
          "dateAdd": "2021-04-09 13:19:57",
          "goodsId": 805720,
          "goodsName": "爆款背心"
        },
        {
          "nick": "毛衣",
          "uid": 1942058,
          "number": 1,
          "yyId": 10024402222164,
          "orderId": 859351,
          "avatarUrl": "https://cdn.it120.cc/apifactory/2019/06/26/aa3633df-62f2-4ff2-8773-ec546b4eabe4.jpg",
          "dateAdd": "2021-04-09 13:19:57",
          "goodsId": 805719,
          "supplyType": "cps_jd",
          "goodsName": "女孩子喜欢的圈圈毛衣，加绒"
        },
        {
          "nick": "叁",
          "uid": 1939888,
          "number": 1,
          "orderId": 859214,
          "avatarUrl": "https://cdn.it120.cc/apifactory/2019/06/26/64b2cbba-b3cd-4d9c-87e7-4fce4ffe3ec6.jpg",
          "dateAdd": "2021-04-09 10:23:12",
          "goodsId": 805718,
          "goodsName": "毛衣"
        }
      ],
      "msg": "success"
    },
    xjh_jGG: [],
    xjh_YHZU: [],
    xjh_XSMS: [],
    xjh_YHZU: [],
    hhh: "",
    mmm: "",
    sss: "",
    currentValue: 5,
    pageSize: 10,
    page: 1,
  },
  onClickTm() {
    wx.navigateTo({
      url: '/pages/TMsale/TMsale',
    })
  },
  onclickMM(e) {
    let xjh_id = e.currentTarget.dataset.id.id
    console.log(xjh_id);
    wx.reLaunch({
      url: "/pages/logs/logs?id=" + xjh_id,
    })
  },
  onDrag(event) {
    this.setData({
      currentValue: event.detail.value,
    });
  },
  onShow() {
    getBanner().then((res) => {
      this.data.list = res
      this.setData({
        xjh_list: this.data.list
      })
    }).catch(err => {
      console.log(err);
    })
    homeJGG().then((res) => {
        // console.log(res);
        this.data.xjh_jGG = res
        this.setData({
          xjh_jGG: this.data.xjh_jGG
        })
      }),
      xjh_YHZU().then((res) => {
        console.log(res);
        this.data.xjh_YHZU = res.dataList[0]
        console.log(this.data.xjh_YHZU);
        this.setData({
          xjh_YHZU: this.data.xjh_YHZU
        })
      }),
      xjh_XSMS().then((res) => {
        // console.log(res);
        this.data.xjh_XSMS = res
        this.setData({
          xjh_XSMS: this.data.xjh_XSMS
        })
      })
    fzf_baopin().then((res) => {
        // console.log(res);
        this.data.fzf_baopin = res;
        this.setData({
          fzf_baopin: this.data.fzf_baopin
        })
      }),
      fzf_kanjia().then((res) => {
        // console.log(res);
        this.data.fzf_kanjia = res;
        this.setData({
          fzf_kanjia: this.data.fzf_kanjia
        })
      })
  },
  onClickIndex(e) {
    let goodsId = e.currentTarget.dataset.uid.goodsId;
    // console.log(goodsId);
    wx.navigateTo({
      url: '/pages/commodity/commodity?id=' + goodsId,
    })

  },
  onClickOUt() {
    wx.navigateTo({
      url: '/pages/xjh_search/xjh_search',
    })


  },
  onClickXSMS(e) {
    // console.log(e);
    this.data.itemID = e.currentTarget.dataset.tap.id
    // console.log(this.data.itemID);
    wx.navigateTo({
      url: '/pages/commodity/commodity?id=' + this.data.itemID
    })
  },
  // 优惠资讯事件
  onClickYHZX(e) {
    console.log(e);
    this.data.xjh_GGXQ=e.currentTarget.dataset.tab
    wx.navigateTo({
      url: '/pages/xjh_Ggxq/xjh_Ggxq?id='+this.data.xjh_GGXQ,
    })
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onPullDownRefresh: function () {
    // 触发下拉刷新时执行
    console.log('刷新');
    this.setData({
      fzf_show: true,
    })
    this.setData({
      fzf_iSshow: true,
    })
    setInterval(() => {
      this.setData({
        fzf_iSshow: false,
      })
    }, 1000)
  },
  onReachBottom: function () {
    fzf_categories(this.data.pageSize).then((res) => {
      this.data.fzf_listen = res;
      if (this.data.pageSize <= res.length) {
        this.data.page++;
      } else {
        this.setData({
          fzf_shows: true,
        })
        setInterval(() => {
          this.setData({
            fzf_shows: false,
          })
        }, 5000)
      }
      this.data.pageSize = this.data.page * this.data.pageSize;
      this.setData({
        page: this.data.page,
        pageSize: this.data.pageSize,
        fzf_listen: this.data.fzf_listen,
      })
    })
  },

  count_down: function (countDown_time) {
    var that = this;
    var time = countDown_time.split(':')
    var hhh = parseInt(time[0])
    var mmm = parseInt(time[1])
    var sss = parseInt(time[2])
    this.setData({
      sss: (sss < 10) ? '0' + sss : sss,
      mmm: (mmm < 10) ? '0' + mmm : mmm,
      hhh: (hhh < 10) ? '0' + hhh : hhh
    })
    var Interval = setInterval(function () {
      if (sss > 0) {
        sss--
      } else {
        console.log('时间到')
        clearInterval(Interval)
      }
      if (sss == 0) {
        if (mmm > 0) {
          mmm--
          sss = 59;
        }
        if (mmm == 0 && hhh > 0) {
          hhh--
          sss = 59;
          mmm = 59;
        }
      }
      that.setData({
        sss: (sss < 10) ? '0' + sss : sss,
        mmm: (mmm < 10) ? '0' + mmm : mmm,
        hhh: (hhh < 10) ? '0' + hhh : hhh
      })
    }, 1000)
  },
  onLoad() {
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
    fzf_categories(this.data.pageSize).then((res) => {
        // console.log(res);
        this.data.fzf_listen = res;
        this.setData({
          fzf_listen: this.data.fzf_listen,
        })
      }),
      pintuan().then((res) => {
        this.data.fzf_pt = res;
        this.setData({
          fzf_pt: this.data.fzf_pt,
        })
      }),
      xjh_XSMS().then((res) => {
        // console.log(res);

        var timedifference = function (faultDate, completeTime) {
          var stime = Date.parse(new Date(faultDate)); //获得开始时间的毫秒数
          var etime = Date.parse(new Date(completeTime)); //获得结束时间的毫秒数
          var usedTime = etime - stime; //两个时间戳相差的毫秒数
          var days = Math.floor(usedTime / (24 * 3600 * 1000));
          //计算出小时数
          var leave1 = usedTime % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
          var hours = Math.floor(leave1 / (3600 * 1000)); //将剩余的毫秒数转化成小时数
          //计算相差分钟数
          var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
          var minutes = Math.floor(leave2 / (60 * 1000)); //将剩余的毫秒数转化成分钟
          //计算相差秒数
          var leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数
          var seconds = Math.floor(leave3 / 1000); //将剩余的毫秒数转化成秒数

          var dayStr = days == 0 ? "" : days + "d:";
          var hoursStr = hours == 0 ? "" : hours + "h:";
          var minutesStr = minutes == 0 ? "" : minutes + "m:"
          var time = dayStr + hoursStr + minutesStr + seconds + "s";
          return time;
        };


        var countDown_time = timedifference(res[0].dateStart, res[0].dateUpdate);
        this.count_down(countDown_time);
        this.data.xjh_XSMS = res;
        this.setData({
          xjh_XSMS: this.data.xjh_XSMS
        })
      })
  },
  getUserProfile(e) {
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    console.log(e)
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }

})